﻿// "Therefore those skilled at the unorthodox
// are infinite as heaven and earth,
// inexhaustible as the great rivers.
// When they come to an end,
// they bagin again,
// like the days and months;
// they die and are reborn,
// like the four seasons."
// 
// - Sun Tsu,
// "The Art of War"


namespace Infrastructure.Logging
{
    /// <summary>
    /// Implementation of Properties collection for the ThreadContext.
    /// </summary>
    /// <remarks>
    /// Class implements a collection of properties that is specific to each thread. 
    /// The class is not synchronized as each thread has its own PropertiesDictionary. 
    /// </remarks>
    public class ThreadContextProperties
    {
        /// <summary>
        /// create proxy for real property collection
        /// </summary>
        /// <param name="props">real property collection</param>
        public ThreadContextProperties( log4net.Util.ThreadContextProperties props )
        {
            mProps = props;
        }

        /// <summary>
        ///             Remove a property
        /// </summary>
        /// <param name="key">the key for the entry to remove</param>
        /// <remarks>
        /// <para>
        ///             Remove a property
        /// </para>
        /// </remarks>
        public void Remove( string key )
        {
            mProps.Remove( key );
        }

        /// <summary>
        ///             Clear all properties
        /// </summary>
        /// <remarks>
        /// <para>
        ///             Clear all properties
        /// </para>
        /// </remarks>
        public void Clear()
        {
            mProps.Clear();
        }

        /// <summary>
        ///             Gets or sets the value of a property
        /// </summary>
        /// <value>
        ///             The value for the property with the specified key
        /// </value>
        /// <remarks>
        /// <para>
        ///             Gets or sets the value of a property
        /// </para>
        /// </remarks>
        public object this[ string key ]
        {
            get
            {
                var obj = mProps[key];
                if (obj is FixingWrapper)
                {
                    obj = ((FixingWrapper)obj).Object;
                }
                return obj;
            }
            set { mProps[key] = value is IFixingRequired ? new FixingWrapper((IFixingRequired)value) : value; }
        }

        /// <summary>
        /// real property collection
        /// </summary>
        private readonly log4net.Util.ThreadContextProperties mProps;
    }
}
